﻿
/******************************************************************************
 * Product: Adempiere ERP & CRM Smart Business Solution                        *
 * Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved.                *
 * This program is free software; you can redistribute it and/or modify it    *
 * under the terms version 2 of the GNU General Public License as published   *
 * by the Free Software Foundation. This program is distributed in the hope   *
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.           *
 * See the GNU General Public License for more details.                       *
 * You should have received a copy of the GNU General Public License along    *
 * with this program; if not, write to the Free Software Foundation, Inc.,    *
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.                     *
 * For the text or an alternative of this public license, you may reach us    *
 * ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA        *
 * or via info@compiere.org or http://www.compiere.org/license.html           *
 *****************************************************************************/

/**
 *	System Display Types.
 *  <pre>
 *	SELECT AD_Reference_ID, Name FROM AD_Reference WHERE ValidationType = 'D'
 *  </pre>
 *  @author     Jorg Janke
 *  @version    $Id: DisplayType.java,v 1.6 2006/08/30 20:30:44 comdivision Exp $
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace org.compiere.util
{
    struct DisplayType
    {
        	/** Display Type 10	String	*/
	public static readonly int String     = 10;
	/** Display Type 11	Integer	*/
	public static readonly int Integer    = 11;
	/** Display Type 12	Amount	*/
	public static readonly int Amount     = 12;
	/** Display Type 13	ID	*/
	public static readonly int ID         = 13;
	/** Display Type 14	Text	*/
	public static readonly int Text       = 14;
	/** Display Type 15	Date	*/
	public static readonly int Date       = 15;
	/** Display Type 16	DateTime	*/
	public static readonly int DateTime   = 16;
	/** Display Type 17	List	*/
	public static readonly int List       = 17;
	/** Display Type 18	Table	*/
	public static readonly int Table      = 18;
	/** Display Type 19	TableDir	*/
	public static readonly int TableDir   = 19;
	/** Display Type 20	YN	*/
	public static readonly int YesNo      = 20;
	/** Display Type 21	Location	*/
	public static readonly int Location   = 21;
	/** Display Type 22	Number	*/
	public static readonly int Number     = 22;
	/** Display Type 23	BLOB	*/
	public static readonly int Binary     = 23;
	/** Display Type 24	Time	*/
	public static readonly int Time       = 24;
	/** Display Type 25	Account	*/
	public static readonly int Account    = 25;
	/** Display Type 26	RowID	*/
	public static readonly int RowID      = 26;
	/** Display Type 27	Color   */
	public static readonly int Color      = 27;
	/** Display Type 28	Button	*/
	public static readonly int Button	   = 28;
	/** Display Type 29	Quantity	*/
	public static readonly int Quantity   = 29;
	/** Display Type 30	Search	*/
	public static readonly int Search     = 30;
	/** Display Type 31	Locator	*/
	public static readonly int Locator    = 31;
	/** Display Type 32 Image	*/
	public static readonly int Image      = 32;
	/** Display Type 33 Assignment	*/
	public static readonly int Assignment = 33;
	/** Display Type 34	Memo	*/
	public static readonly int Memo       = 34;
	/** Display Type 35	PAttribute	*/
	public static readonly int PAttribute = 35;
	/** Display Type 36	CLOB	*/
	public static readonly int TextLong   = 36;
	/** Display Type 37	CostPrice	*/
	public static readonly int CostPrice  = 37;
	/** Display Type 36	File Path	*/
	public static readonly int FilePath  = 38;
	/** Display Type 39 File Name	*/
	public static readonly int FileName  = 39;
	/** Display Type 40	URL	*/
	public static readonly int URL  = 40;
	/** Display Type 42	PrinterName	*/
	public static readonly int PrinterName  = 42;






        	//	Candidates: 
	
	/**
	 *	- New Display Type
		INSERT INTO AD_REFERENCE
		(AD_REFERENCE_ID, AD_CLIENT_ID,AD_ORG_ID,ISACTIVE,CREATED,CREATEDBY,UPDATED,UPDATEDBY,
		NAME,DESCRIPTION,HELP, VALIDATIONTYPE,VFORMAT,ENTITYTYPE)
		VALUES (35, 0,0,'Y',SysDate,0,SysDate,0,
		'PAttribute','Product Attribute',null,'D',null,'D');
	 *
	 *  - org.compiere.model.MModel (??)
	 *	- org.compiere.grid.ed.VEditor/Dialog
	 *	- org.compiere.grid.ed.VEditorFactory
	 *	- RColumn, WWindow
	 *  add/check 0_cleanupAD.sql
	 */

	//  See DBA_DisplayType.sql ----------------------------------------------

	/** Maximum number of digits    */
	private static readonly int    MAX_DIGITS = 28;        //  Oracle Standard Limitation 38 digits
	/** Digits of an Integer        */
	private static readonly int    INTEGER_DIGITS = 10;
	/** Maximum number of fractions */
	private static readonly int    MAX_FRACTION = 12;
	/** Default Amount Precision    */
	private static readonly int    AMOUNT_FRACTION = 2;

	/**
	 *	Returns true if (numeric) ID (Table, Search, Account, ..).
	 *  (stored as Integer)
	 *  @param displayType Display Type
	 *  @return true if ID
	 */
	public static bool isID (int displayType)
	{
		if (displayType == ID || displayType == Table || displayType == TableDir
			|| displayType == Search || displayType == Location || displayType == Locator
			|| displayType == Account || displayType == Assignment || displayType == PAttribute
			|| displayType == Image)
			return true;
		return false;
	}	//	isID

	/**
	 *	Returns true, if DisplayType is numeric (Amount, Number, Quantity, Integer).
	 *  (stored as BigDecimal)
	 *  @param displayType Display Type
	 *  @return true if numeric
	 */
	public static bool isNumeric(int displayType)
	{
		if (displayType == Amount || displayType == Number || displayType == CostPrice 
			|| displayType == Integer || displayType == Quantity)
			return true;
		return false;
	}	//	isNumeric
	
	/**
	 * 	Get Default Precision.
	 * 	Used for databases who cannot handle dynamic number precision.
	 *	@param displayType display type
	 *	@return scale (decimal precision)
	 */
	public static int getDefaultPrecision(int displayType)
	{
		if (displayType == Amount)
			return 2;
		if (displayType == Number)
			return 6;
		if (displayType == CostPrice 
			|| displayType == Quantity)
			return 4;
		return 0;
	}	//	getDefaultPrecision
	

	/**
	 *	Returns true, if DisplayType is text (String, Text, TextLong, Memo).
	 *  @param displayType Display Type
	 *  @return true if text
	 */
	public static bool isText(int displayType)
	{
		if (displayType == String || displayType == Text 
			|| displayType == TextLong || displayType == Memo
			|| displayType == FilePath || displayType == FileName
			|| displayType == URL || displayType == PrinterName)
			return true;
		return false;
	}	//	isText

	/**
	 *	Returns truem if DisplayType is a Date.
	 *  (stored as Timestamp)
	 *  @param displayType Display Type
	 *  @return true if date
	 */
	public static bool isDate (int displayType)
	{
		if (displayType == Date || displayType == DateTime || displayType == Time)
			return true;
		return false;
	}	//	isDate

	/**
	 *	Returns true if DisplayType is a VLookup (List, Table, TableDir, Search).
	 *  (stored as Integer)
	 *  @param displayType Display Type
	 *  @return true if Lookup
	 */
	public static bool isLookup(int displayType)
	{
		if (displayType == List || displayType == Table
			|| displayType == TableDir || displayType == Search)
			return true;
		return false;
	}	//	isLookup
	
	/**
	 * 	Returns true if DisplayType is a Large Object
	 *	@param displayType Display Type
	 *	@return true if LOB
	 */
	public static bool isLOB (int displayType)
	{
		if (displayType == Binary 
			/*
			 * Removed by comdivisionys as Image is not a real
			 * LOB Type. Tested the behavior with Image window
			 * and Web Project Media. Both worked fine after 
			 * the remove.
			 */
			//|| displayType == Image
			|| displayType == TextLong)
			return true;
		return false;
	}

    ///**************************************************************************
    // *	Return Format for numeric DisplayType
    // *  @param displayType Display Type (default Number)
    // *  @param language Language
    // *  @return number format
    // */
    //public static DecimalFormat getNumberFormat(int displayType, Language language)
    //{
    //    Language myLanguage = language;
    //    if (myLanguage == null)
    //        myLanguage = Language.getLoginLanguage();
    //    Locale locale = myLanguage.getLocale();
    //    DecimalFormat format = null;
    //    if (locale != null)
    //        format = (DecimalFormat)NumberFormat.getNumberInstance(locale);
    //    else
    //        format = (DecimalFormat)NumberFormat.getNumberInstance(Locale.US);
    //    //
    //    if (displayType == Integer)
    //    {
    //        format.setParseIntegerOnly(true);
    //        format.setMaximumIntegerDigits(INTEGER_DIGITS);
    //        format.setMaximumFractionDigits(0);
    //    }
    //    else if (displayType == Quantity)
    //    {
    //        format.setMaximumIntegerDigits(MAX_DIGITS);
    //        format.setMaximumFractionDigits(MAX_FRACTION);
    //    }
    //    else if (displayType == Amount)
    //    {
    //        format.setMaximumIntegerDigits(MAX_DIGITS);
    //        format.setMaximumFractionDigits(AMOUNT_FRACTION);
    //        format.setMinimumFractionDigits(AMOUNT_FRACTION);
    //    }
    //    else if (displayType == CostPrice)
    //    {
    //        format.setMaximumIntegerDigits(MAX_DIGITS);
    //        format.setMaximumFractionDigits(MAX_FRACTION);
    //        format.setMinimumFractionDigits(AMOUNT_FRACTION);
    //    }
    //    else //	if (displayType == Number)
    //    {
    //        format.setMaximumIntegerDigits(MAX_DIGITS);
    //        format.setMaximumFractionDigits(MAX_FRACTION);
    //        format.setMinimumFractionDigits(1);
    //    }
    //    return format;
    //}	//	getDecimalFormat

    ///**
    // *	Return Format for numeric DisplayType
    // *  @param displayType Display Type
    // *  @return number format
    // */
    //public static DecimalFormat getNumberFormat(int displayType)
    //{
    //    return getNumberFormat (displayType, null);
    //}   //  getNumberFormat


    ///*************************************************************************
    // *	Return Date Format
    // *  @return date format
    // */
    //public static SimpleDateFormat getDateFormat()
    //{
    //    return getDateFormat (DisplayType.Date, null);
    //}   //  getDateFormat

    ///**
    // *	Return Date Format
    // *  @param language Language
    // *  @return date format
    // */
    //public static SimpleDateFormat getDateFormat (Language language)
    //{
    //    return getDateFormat (DisplayType.Date, language);
    //}	//	getDateFormat

    ///**
    // *	Return format for date displayType
    // *  @param displayType Display Type
    // *  @return date format
    // */
    //public static SimpleDateFormat getDateFormat (int displayType)
    //{
    //    return getDateFormat (displayType, null);
    //}   //  getDateFormat

    ///**
    // *	Return format for date displayType
    // *  @param displayType Display Type (default Date)
    // *  @param language Language
    // *  @return date format
    // */
    //public static SimpleDateFormat getDateFormat (int displayType, Language language)
    //{
    //    Language myLanguage = language;
    //    if (myLanguage == null)
    //        myLanguage = Language.getLoginLanguage();
    //    //
    //    if (displayType == DateTime)
    //        return myLanguage.getDateTimeFormat();
    //    else if (displayType == Time)
    //        return myLanguage.getTimeFormat();
    ////	else if (displayType == Date)
    //    return myLanguage.getDateFormat();		//	default
    //}	//	getDateFormat

    ///**
    // *	JDBC Date Format YYYY-MM-DD
    // *  @return date format
    // */
    //static public SimpleDateFormat getDateFormat_JDBC()
    //{
    //    return new SimpleDateFormat ("yyyy-MM-dd");
    //}   //  getDateFormat_JDBC

	/**
	 *  Return Storage Class.
	 *  (used for MiniTable)
	 *  @param displayType Display Type
	 *  @param yesNoAsbool - yes or no as bool
	 *  @return class Integer - BigDecimal - Timestamp - String - bool
	 */
	public static Type GetType (int displayType, bool yesNoAsbool)
	{
		if (isText(displayType) || displayType == List)
			return typeof(String);
		else if (isID(displayType) || displayType == Integer)    //  note that Integer is stored as BD
			return typeof(Int32);
		else if (isNumeric(displayType))
			return typeof(Decimal);
		else if (isDate(displayType))
			return typeof(DateTime);
		else if (displayType == YesNo)
			return yesNoAsbool ? typeof(bool): typeof(String);
		else if (displayType == Button)
			return typeof(String);
		else if (isLOB(displayType))	//	CLOB is String
			return typeof(byte[]);
		//
		return typeof(object);
	}   //  getClass

	/**
	 * 	Get Description
	 *	@param displayType display Type
	 *	@return display type description
	 */
	public static String getDescription (int displayType)
	{
		if (displayType == String)
			return "String";
		if (displayType == Integer)
			return "Integer";
		if (displayType == Amount)
			return "Amount";
		if (displayType == ID)
			return "ID";
		if (displayType == Text)
			return "Text";
		if (displayType == Date)
			return "Date";
		if (displayType == DateTime)
			return "DateTime";
		if (displayType == List)
			return "List";
		if (displayType == Table)
			return "Table";
		if (displayType == TableDir)
			return "TableDir";
		if (displayType == YesNo)
			return "YesNo";
		if (displayType == Location)
			return "Location";
		if (displayType == Number)
			return "Number";
		if (displayType == Binary)
			return "Binary";
		if (displayType == Time)
			return "Time";
		if (displayType == Account)
			return "Account";
		if (displayType == RowID)
			return "RowID";
		if (displayType == Color)
			return "Color";
		if (displayType == Button)
			return "Button";
		if (displayType == Quantity)
			return "Quantity";
		if (displayType == Search)
			return "Search";
		if (displayType == Locator)
			return "Locator";
		if (displayType == Image)
			return "Image";
		if (displayType == Assignment)
			return "Assignment";
		if (displayType == Memo)
			return "Memo";
		if (displayType == PAttribute)
			return "PAttribute";
		if (displayType == TextLong)
			return "TextLong";
		if (displayType == CostPrice)
			return "CostPrice";
		if (displayType == FilePath)
			return "FilePath";
		if (displayType == FileName)
			return "FileName";
		if (displayType == URL)
			return "URL";
		if (displayType == PrinterName)
			return "PrinterName";
		//
		return "UNKNOWN DisplayType=" + displayType;
	}	//	getDescription
	

    }
}



